<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt />
        <span>空间预约后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 菜单的收缩和展开 -->
        <div class="toggle-button" @click="toggleColl">|||</div>

        <!-- 侧边栏菜单 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eef" :unique-opened="true"
          :collapse="isCollapse" :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click="saveNavSatte('/' + subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menulist: [
        {
          id: 1, authName: '首页',
          children: [
            { id: 1, authName: '首页', path: 'welcome' },
          ]
        },
        {
          id: 2, authName: '空间管理',
          children: [
            { id: 1, authName: '预约空间', path: 'reservationSpace' },
            { id: 2, authName: '空间分类', path: 'spatialClassification' }
          ]
        },
        {
          id: 3, authName: '预约管理',
          children: [
            { id: 1, authName: '预约审批', path: 'appointmentApproval' },
            { id: 2, authName: '使用记录', path: 'useRecord' },
            { id: 3, authName: '学生信息', path: 'studentInformation' },
            { id: 4, authName: '使用评价', path: 'useEvaluation' },
          ]
        },
        {
          id: 4, authName: '数据统计',
          children: [
            { id: 1, authName: '空间统计', path: 'space' },
            { id: 2, authName: '预约记录', path: 'bespeak' },
            { id: 3, authName: '使用信息', path: 'use' },
          ]
        },
        {
          id: 5, authName: '系统管理',
          children: [
            { id: 1, authName: '用户管理', path: 'user' },
            { id: 2, authName: '角色管理', path: 'role' },
            { id: 3, authName: '菜单管理', path: 'menu' },
            { id: 4, authName: '系统参数', path: 'systemParameter' },
            { id: 5, authName: '通知公告', path: 'noticeAffiche' },
            { id: 6, authName: '投诉建议', path: 'complaintAdvice' },
          ]
        }

      ],
      //一级菜单中图标的样式
      iconObj: {
        '125': 'el-icon-user-solid',
        '103': 'el-icon-star-on',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-success',
        '145': 'el-icon-user-solid',
      },
      //是否折贴
      isCollapse: false,
      //被激活的连接地址
      activePath: ''
    }
  },
  created() {
    // this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
    logout() {
      //清空sessionStorage
      window.sessionStorage.clear()
      //声明式导航跳转到登录页面
      this.$router.push('/login')
    },
    //获取所有的菜单
    // async getMenuList() {
    //   const { data: res } = await this.$http.get('menus')
    //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    //   this.menulist = res.data
    //   console.log(res)
    // },
    //菜单的折叠和张开
    toggleColl() {
      //取反
      this.isCollapse = !this.isCollapse
    },
    //保存连接的激活状态
    saveNavSatte(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  >div {
    display: flex;
    align-items: center;

    img {
      width: 50px;
      height: 50px;
    }
  }
}

.el-aside {
  background: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  // background: #eee;
  height: 100%;
  overflow: auto;
}

.toggle-button {
  background: #cccccc;
  color: #fff;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>